﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class UserControls_NumericBox : System.Web.UI.UserControl
{
    #region Properties
    public override string ClientID
    {
        get
        {
            return this.txtNumericBox.ClientID;
        }
    }

    public string Text
    {
        get { return this.txtNumericBox.Text; }
        set
        {
            this.txtNumericBox.Text = value;
        }
    }

    public bool Enabled
    {
        get { return this.txtNumericBox.Enabled; }
        set { this.txtNumericBox.Enabled = value; }
    }

    public Unit Width
    {
        get { return this.txtNumericBox.Width; }
        set { this.txtNumericBox.Width = value; }
    }

    private string _onblur = String.Empty;

    public string onblur
    {
        get { return _onblur; }
        set { _onblur = value; }
    }
    

    public string CssClass
    {
        set { this.txtNumericBox.CssClass += " " + value; }
    }

    private string _meta = String.Empty;
    public string meta
    {
        set { _meta = value; }
    }

    public int MaxLength
    {
        get { return this.txtNumericBox.MaxLength; }
        set
        {
            if (this.txtNumericBox.MaxLength != value)
            {
                this.txtNumericBox.MaxLength = value;
            }
        }
    } 
    #endregion
    #region Constructors & destructors
    public UserControls_NumericBox()
    {

    } 
    #endregion
    #region Events
    protected override void OnInit(EventArgs e)
    {
        //if (!this.Page.ClientScript.IsClientScriptBlockRegistered("FilterNumeric"))
        //{
        //    // If not register
        //    this.Page.RegisterClientScriptBlock("FilterNumeric", GetNumberValidatorScript());
        //}
        //this.txtNumericBox.Attributes.Add("onkeypress", "FilterNumeric($(this).val())");
        //this.txtNumericBox.Attributes.Add("onblur", "$(this).formatCurrency()");
        if (_meta != String.Empty)
            this.txtNumericBox.Attributes.Add("meta", _meta);

        this.txtNumericBox.Attributes.Add("onfocus", "if(this.value == '0') this.value = '';");
        this.txtNumericBox.Attributes.Add("onblur", "if(this.value == '') this.value = '0';");

        if (_onblur != String.Empty)
            this.txtNumericBox.Attributes["onblur"] += _onblur;

        base.OnInit(e);
    } 
    #endregion
    #region Methods
    /// <summary>
    /// Returns script used to validate numbers
    /// </summary>
    /// <returns></returns>
    protected string GetNumberValidatorScript()
    {
        //#region BUILDS NUMERIC VALIDATION SCRIPT ONKEYPRESS
        string script;
        script = "<script> ";
        script += @"function FilterNumeric(value){";
        script += @"var ch = String.fromCharCode(event.keyCode);";
        script += @"if (event.keyCode < 32)";
        script += @"{";
        script += @"return;";
        script += @"};";

        script += @"if((event.keyCode <= 57)&&(event.keyCode >= 48))";
        script += @"{";
        script += @"if (!event.shiftKey)";
        script += @"{";
        script += @"return;";
        script += @"}";
        script += @"}";
        script += @"if ((ch == '-') || (ch == '.'))";
        script += @"{";
        script += @"if(value.indexOf(ch) < 0)";
        script += @"return;";
        script += @"}";
        script += @"event.returnValue = false;";
        script += "}</script>";

        return script;
    } 
    #endregion
}
